from enum import Enum
from typing import Tuple, TypeVar

EnumT = TypeVar("EnumT", int, str)  # 枚举代码的类型约束


class BaseTupleEnum(Enum):
    """元组枚举基类，支持代码(code)和标签(label)属性"""

    def __init__(self, code: EnumT, label: str):
        """初始化枚举成员"""
        self._code = code
        self._label = label

    @property
    def code(self) -> EnumT:
        """获取枚举代码"""
        return self._code

    @property
    def label(self) -> str:
        """获取枚举标签"""
        return self._label

    def __str__(self) -> str:
        """返回枚举成员的字符串表示"""
        return f"{self.name} ({self.code}, {self.label})"

    @classmethod
    def from_code(cls, code: EnumT) -> "BaseTupleEnum":
        """通过代码查找枚举成员"""
        for member in cls:
            if member.code == code:
                return member
        raise ValueError(f"'{code}' 不是有效的 {cls.__name__} 代码")

    @classmethod
    def from_label(cls, label: str) -> "BaseTupleEnum":
        """通过标签查找枚举成员"""
        for member in cls:
            if member.label == label:
                return member
        raise ValueError(f"'{label}' 不是有效的 {cls.__name__} 标签")

    @property
    def value(self) -> Tuple[EnumT, str]:
        """返回原始值（元组）"""
        return (self.code, self.label)

    @classmethod
    def codes(cls) -> Tuple[EnumT, ...]:
        """获取所有枚举代码"""
        return tuple(member.code for member in cls)

    @classmethod
    def labels(cls) -> Tuple[str, ...]:
        """获取所有枚举标签"""
        return tuple(member.label for member in cls)

    @classmethod
    def choices(cls) -> Tuple[Tuple[EnumT, str], ...]:
        """获取适用于Django等框架的choices选项"""
        return tuple((member.code, member.label) for member in cls)
